//
//  MHTopicMoreBookListCollectionViewController.m
//  ManHua
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHTopicMoreBookListCollectionViewController.h"
#import "MHComicIntroductionCell.h"
#import "MHComicDetailViewController.h"
#import "MHTopicBannerModel.h"
#import "MHComicIntroduct.h"
#import "MHTopicMoreBookHeaderView.h"
#import <AFNetworking.h>
@interface MHTopicMoreBookListCollectionViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)NSMutableArray *datasoureArr;
@property(nonatomic,strong)UICollectionView *collectionView;


@end

@implementation MHTopicMoreBookListCollectionViewController

-(NSMutableArray *)datasoureArr
{
    if (_datasoureArr==nil) {
        self.datasoureArr=[NSMutableArray arrayWithCapacity:0];
        
    }
    return _datasoureArr;
}
-(void)acquireData
{ NSMutableDictionary *parDic=[NSMutableDictionary dictionary];
    parDic[@"method"]=@"search";
    parDic[@"type"]=@"2";
    if (self.bannerModel)
{
      
    parDic[@"name"]=self.bannerModel.BookIDs ;
  
}
    else
    {
        parDic[@"name"]=self.mainTopicModel.MoreIDs;
    }
    [[AFHTTPRequestOperationManager manager] GET:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx" parameters:parDic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
      
        [responseObject enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MHComicIntroduct *commicIntroduce=[[MHComicIntroduct alloc]init];
            [commicIntroduce setValuesForKeysWithDictionary:obj];
            [self.datasoureArr   addObject:commicIntroduce];
        }];
                [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"专题更多数据请求失败错误原因%@",error.localizedDescription);
    }];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind ==UICollectionElementKindSectionHeader) {
        MHTopicMoreBookHeaderView *resusableView=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        resusableView.weburl=self.bannerModel.TopicDescription;
        resusableView.changeHeaderSectionFrameBlock=^(CGFloat hight)
        {
                    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
                    flowLayout.headerReferenceSize = CGSizeMake(Width, hight);

        };
      return resusableView;
    }
    else
    {
    return nil;
    }
  
}

- (void)viewDidLoad {
    [super viewDidLoad];

         [self acquireData];

   
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)collectionViewLayout:flowlayout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.backgroundColor=[UIColor whiteColor];
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:@"MHComicIntroductionCell"];
    if (self.bannerModel.TopicDescription!=nil&&![self.bannerModel.TopicDescription isEqualToString:@""]) {
        //            MHTopicMoreBookHeaderView *headerView=[[MHTopicMoreBookHeaderView alloc]init];
        //            headerView.weburl=self.bannerModel.TopicDescription;
        
        
        UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
       
        flowLayout.headerReferenceSize = CGSizeMake(Width, Height);
        flowLayout.minimumLineSpacing = 10;
        [self.collectionView registerClass:[MHTopicMoreBookHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        
    }

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return self.datasoureArr.count;








   }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    MHComicIntroductionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHComicIntroductionCell" forIndexPath:indexPath];

         cell.comicIntroduct=self.datasoureArr[indexPath.row];


   
    // Configure the cell
    
    
    return cell;
}
#pragma mark -<UICollectionViewFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat padding=10;
     NSInteger row=3;
    CGFloat ItemW=(Width-2*padding-(row-1)*padding)/3;
    CGFloat ItemY=(ItemW-padding)*11.0/9+35;
    
    return CGSizeMake(ItemW, ItemY);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicDetailViewController *comicDetailCVC=[[MHComicDetailViewController alloc]init];
    comicDetailCVC.comicIntroduct = self.datasoureArr[indexPath.row];
    [self.navigationController pushViewController:comicDetailCVC animated:YES];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
